package com.mmall.controller.backend;

import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.utils.PropertiesUtil;
import net.sf.jsqlparser.schema.Server;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by chenxu on 17/7/27.
 */
@Controller
@RequestMapping(value = "/manage/product")
public class ProductManageController {

    @Autowired
    IProductService productService;

    @Autowired
    IFileService fileService;

    @RequestMapping(value = "save.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("没有操作权限");
        }

        // 填充增加产品的service
        return productService.saveOrUpdateProduct(product);
    }

    @RequestMapping(value = "set_sale_status.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setSaleStatus(HttpSession session, Integer productId, Integer status){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("没有操作权限");
        }

        // 填充增加产品的service
        return productService.setSaleStatus(productId,status);
    }

    @RequestMapping(value = "detail.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getDetail(HttpSession session, Integer productId){

        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("没有操作权限");
        }
        return productService.manageProductDetail(productId);
    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse list(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("没有操作权限");
        }

        return productService.getProductList(pageNum, pageSize);
    }

    @RequestMapping(value = "search.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse produdctSearch(HttpSession session, String productName, Integer productId, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("没有操作权限");
        }
        return productService.searchProduct(productId,productName,pageNum, pageSize);
    }

    @RequestMapping(value = "upload.do")
    @ResponseBody
    public ServerResponse upload(HttpSession session, @RequestParam(value = "uploadFile",required = false)MultipartFile file, HttpServletRequest request){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("没有操作权限");
        }

        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = fileService.upload(file, path);
        String url = PropertiesUtil.getProperty("")+targetFileName;

        Map fileMap = Maps.newHashMap();
        fileMap.put("uri", targetFileName);
        fileMap.put("url",url);
        return ServerResponse.createBySuccess(fileMap);
    }

    @RequestMapping(value = "richtext_image_upload.do")
    @ResponseBody
    public Map richtextImageUpload(HttpSession session, MultipartFile file, HttpServletRequest request, HttpServletResponse response)
    {
        Map resultMap = Maps.newHashMap();
        // check user
        // check admin
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            resultMap.put("success","false");
            resultMap.put("msg","请先登录");
            return resultMap;
        }

        if (user.getRole()!=Const.Role.ROLE_ADMIN){
            resultMap.put("success","false");
            resultMap.put("msg", "没有管理员权限");
            return resultMap;
        }

        // upload
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = fileService.upload(file, path);
        if (StringUtils.isBlank(targetFileName)){
            resultMap.put("success","false");
            resultMap.put("msg", "上传失败");
            return resultMap;
        }
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        // add response header, x-file

        resultMap.put("success","true");
        resultMap.put("msg", "文件上传成功");
        resultMap.put("file_path",url);

        response.addHeader("Access-Controll-Allow-Headers","X-File-Name");
        return resultMap;
    }
}